﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application
{
    /// <summary>
    /// 创建连接处理
    /// </summary>
    public class DbComponent
    {
        /// 数据库连接配置
        private static ConnectionStringSettings connectionSetting;

        public static void InitDapper(ConnectionStringSettings connectionStringSettings)
        {
            connectionSetting = connectionStringSettings;
        }

        //通过工厂模式创建Connection连接 此连接已打开
        public static IDbConnection Connection
        {
            get {
                var cnnection = DbProviderFactories.GetFactory(connectionSetting.ProviderName).CreateConnection();
                if (cnnection == null)
                    throw new Exception("数据库链接获取失败!");
                cnnection.ConnectionString = connectionSetting.ConnectionString;
                cnnection.Open();
                return cnnection;
            }
        }
    }
}
